<?php
    //if($_GET["username"] && $_GET["password"] )
    //{

    $host=":3306"; // Host name 
    $username_db="infinity"; // Mysql username 
    $password_db=""; // Mysql password 
    $db_name="pervasive_infinity"; // Database name 
    $tbl_name="users"; // Table name

    $conn = mysql_connect("$host", "$username_db", "$password_db")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");


    $username=$_POST["username"];
    $checkuser = mysql_query("SELECT username FROM users WHERE username='$username'");

    $username_exist = mysql_num_rows($checkuser);

    if($username_exist > 0 ){
        echo "Username already exist. Try again.";
        unset($username);
        print "<a href='registration_f.php'>Try Again</a>";
        //include 'register.html';
        exit();
    }else if($username_exist == 0){
            if ((($_FILES["file"]["type"] == "image/gif")
            || ($_FILES["file"]["type"] == "image/jpeg")
            || ($_FILES["file"]["type"] == "image/pjpeg"))
            && ($_FILES["file"]["size"] < 20000))
            {
                if ($_FILES["file"]["error"] > 0)
                {
                    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
                     print "<a href='registration_f.php'>Try Again</a>";
                    exit();
                }
                else
                {
                    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
                    echo "Type: " . $_FILES["file"]["type"] . "<br />";
                    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
                    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

                    if (file_exists("upload/" . $_FILES["file"]["name"]))
                    {
                        echo $_FILES["file"]["name"] . " already exists. ";
                         print "<a href='registration_f.php'>Try Again</a>";
                        exit();
                    }
                    else
                    {
                        move_uploaded_file($_FILES["file"]["tmp_name"],
                        "upload/" . $_FILES["file"]["name"]);
                        echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
                        $image="upload/" . $_FILES["file"]["name"];
                         
                    }
                }
            }
            else
            {
                echo "Invalid file";
                 print "<a href='registration_f.php'>Try Again</a>";
                exit();
            }
            $sql="insert into users (username,password,sex,image,loc_ID)values('$_POST[username]','$_POST[password]','$_POST[sex]','$image','$_POST[loc_ID]')";
            $result=mysql_query($sql,$conn) or die(mysql_error());
            print "<h1>you have registered sucessfully</h1>";
            print "<a href='main_login.php'>go to login page</a>";
        }




        //}
        //else print"invaild data";
?>